package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.hosp.result.R;
import com.atguigu.yygh.hosp.util.JwtHelper;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 就诊人的相关操作
 */
@RestController
@RequestMapping("/api/user/patient")
public class PatientController {
    @Autowired
    PatientService patientService;

    @Autowired
    UserInfoService userInfoService;

    @GetMapping("/getUserPhoe/{patientId}")
    public String getUserPhone(@PathVariable("patientId") Long patientId){
        Patient patient = patientService.getById(patientId);
        Long userId = patient.getUserId();
        UserInfo userInfo = userInfoService.getByUserId(userId);
        return userInfo.getPhone();
    }

    /**
     * 根据就诊人id查询就诊人信息
     */
    @GetMapping("/getPatient/{patientId}")
    public Patient getPatient(@PathVariable Long patientId){
        return patientService.findById(patientId);
    }

    /**
     * 根据就诊人id删除就诊人
     */
    @DeleteMapping("/removeById/{id}")
    public R removeById(@PathVariable("id") Long id){
        patientService.removeById(id);
        return R.ok();
    }

    /**
     * 根据id改就诊人
     */
    @PostMapping("/updateById")
    public R updateById(@RequestBody Patient patient){
        patientService.updateById(patient);
        patient.setUpdateTime(new Date());
        return R.ok();
    }

    /**
     * 根据就诊人id查询
     */
    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Long id){
        Patient patient = patientService.findById(id);
        return R.ok().data("patient",patient);
    }

    /**
     * 添加就诊人
     * 就诊人是哪个用户下的,所以要在token里面解析userid
     * 1.注册账号(一个用户)最多可以同时绑定四个就诊人
     * 2.注册账号每自然年内,最多可以绑定八位就诊人
     * 3.同一手机号,最多同时可以被八位就诊人作为联系电话
     */
    @PostMapping("/savePatient")
    public R save(@RequestBody Patient patient,HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        if(userId!=null){
            patient.setUserId(userId);
            patientService.addPatient(patient);
        }
        return R.ok();

    }


    /**
     * 查询当前用户的就诊人列表
     * (1)从请求头中获取
     */
    @GetMapping("/findPatientList")
    public R findAll(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtHelper.getUserId(token);
        if(userId!=null){
           List<Patient> list = patientService.findPatientList(userId);
            return R.ok().data("list",list);
        }
        return R.ok().data("list",null);
    }

    @GetMapping("/findPatientList/{userId}")
    public R findAll(@PathVariable("userId") Long userId){
        if(userId!=null){
            List<Patient> list = patientService.findPatientList(userId);
            return R.ok().data("list",list);
        }
        return R.ok().data("list",null);
    }

}
